<template>
	<view>
		<!-- 搜索区域 -->
		<view class="search-box">
			<my-search @click="gotoSearch"></my-search>
		</view>
		<!-- 轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="true">
			<swiper-item v-for="(item,index) in swiperList" :key="index">
				<navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>

		<!-- 分类导航 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item ,index) in navList" :key="index" @click="navClickHandler(item)">
				<img :src="item.image_src" alt="">
			</view>
		</view>

		<!-- 楼层区域 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item,index) in floorList" :key="index">
				<!-- 1.楼层的标题区域 -->
				<img :src="item['floor_title'].image_src" class="floor-title">
				<!-- 2.楼层的图片区域 -->
				<view class="floor-img-box">
					<!-- 3.左侧的大图片 -->
					<navigator class="left-img-box" :url="item.product_list[0].url">
						<image :src="item.product_list[0].image_src"
							:style="{width:item.product_list[0].image_width+'rpx'}" mode="widthFix"></image>
					</navigator>
					<!-- 4.右侧的4个小图片 -->
					<view class="right-img-box">
						<navigator class="right-img-item" v-for="(item1,index1) in item.product_list" :key="index1"
							:url="item1.url">
							<image :src="item1.image_src" mode="" v-if="index1!==0"
								:style="{width:item1.image_width+'rpx'}" mode="widthFix"></image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import bage from '@/mixins/tabbar-bage.js'
	export default {
		data() {
			return {
				swiperList: [],
				navList: [],
				floorList: []
			};
		},
		methods: {
			// 获取轮播图
			async getSwiperList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/swiperdata')
				if (res.meta.status !== 200) {
					return uni.$showMsg()
				} else {
					this.swiperList = res.message
				}
			},
			// 获取导航条数据
			async getNabList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/catitems')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.navList = res.message
			},
			// 分类导航的点击事件
			navClickHandler(item) {
				if (item.name == "分类") return uni.switchTab({
					url: '/pages/cate/cate'
				})
				if (item.name == "秒杀拍") return uni.switchTab({
					url: '/pages/cate/cate'
				})
				if (item.name == "超市购") return uni.switchTab({
					url: '/pages/cate/cate'
				})
				if (item.name == "母婴品") return uni.switchTab({
					url: '/pages/cate/cate'
				})
			},
			//  获取楼层数据
			async getFloorList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/floordata')
				if (res.meta.status !== 200) return uni.$showMsg()
				// 数据处理
				res.message.forEach((floor) => {
					floor.product_list.forEach((prod) => {
						prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
					})
				})
				this.floorList = res.message
			},
			// 点击搜索事件
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		},
		onLoad() {
			this.getSwiperList()
			this.getNabList()
			this.getFloorList()
		},
		mixins: [bage],
	}
</script>

<style lang="scss">
	swiper {
		height: 330rpx;

		.swiper-item,
		image {
			height: 100%;
			width: 100%;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 15px 0;

		.nav-item img {
			width: 128rpx;
			height: 140rpx;
		}
	}

	.floor-list {

		.floor-item .floor-title {
			width: 100%;
			height: 60rpx;
		}

		.right-img-box {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-around;
		}
	}

	.floor-img-box {
		display: flex;
		padding-left: 10rpx
	}

	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}
</style>
